package com.springboot.service.system;

import com.springboot.entity.TenantDabaseInfoEntity;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.util.jdbc.MySqlJdbcUtil;
import com.springboot.returndata.vo.TenantDabaseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springboot.dao.TenantDabaseInfoMapper;

import java.sql.*;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyj
 * @since 2021-03-17
 */
@Service
public class TenantDabaseInfoService extends ServiceImpl<TenantDabaseInfoMapper, TenantDabaseInfoEntity> {

    @Autowired
    TenantDabaseInfoMapper tenantDabaseInfoMapper;
    @Autowired
    MySqlJdbcUtil mySqlJdbcUtil;


    /**
     * @Description 获取信息
     * @Author zyj
     * @Date 2021/3/17 17:52
     * @param
     * @return java.util.Map<java.lang.String, java.util.List < java.util.Map>>
     **/
    public Map<String, List<Map>> queryTest(){
        List<TenantDabaseInfoVO> tenantDabaseInfoVOS = tenantDabaseInfoMapper.querAllDataBase();
        Map<String, List<Map>> resultMap = new HashMap<>();
        for(TenantDabaseInfoVO tenantDabaseInfoVO : tenantDabaseInfoVOS) {
            Connection connection = mySqlJdbcUtil.getConnection(tenantDabaseInfoVO.getTenantDatabaseName(), tenantDabaseInfoVO.getTenantDatabasePasswd(), tenantDabaseInfoVO.getDatabaseUrl(),
                    tenantDabaseInfoVO.getDatabasePort(), tenantDabaseInfoVO.getTenantDatabase(), tenantDabaseInfoVO.getDrivername());
            if(null == connection){
                continue;
            }
            String sql = "SELECT * FROM `test` t WHERE t.card_type='9'";
            List<String> keyList = new ArrayList<>();
            keyList.add("id");
            keyList.add("record_number");
            keyList.add("_name");
            keyList.add("admin_name");
            keyList.add("card_number");
            keyList.add("card_type");
            List<Map> mapList = mySqlJdbcUtil.selectSQLByJDBC(connection, sql, keyList);
            resultMap.put(String.valueOf(tenantDabaseInfoVO.getTenantid()), mapList);
        }


        return resultMap;
    }
}
